package com.zyzo.demo.appengine.server;

import java.util.ArrayList;
import java.util.List;

import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import org.restlet.security.Role;

import com.zyzo.demo.appengine.model.ServerReponse;
import com.zyzo.demo.appengine.model.User;
import com.zyzo.demo.appengine.model.UserDao;

public class UserController extends ServerResource implements UserControllerInterface 
{
    /** Current user. */
    org.restlet.security.User user;

    /** Its role(s). */
    List<Role> roles;

    @Override
    protected void doInit() throws ResourceException {
        this.user = getClientInfo().getUser();
        this.roles = getClientInfo().getRoles();
    }
	
	@Override
	public ServerReponse create(User user) 
	{ 
		try {
			UserDao.getInstance().createUser( user  );
		} catch (Exception e) {
			return new ServerReponse( ServerReponse.STATUS_ERR , "unable to create user : "+e.getMessage() );
		}
		return new ServerReponse( ServerReponse.STATUS_OK , "user successfully created" );
	 }

	@Override
	public ServerReponse update(User user) 
	{ 
		try {
			UserDao.getInstance().updateUser( user );
		} catch (Exception e) {
			return new ServerReponse( ServerReponse.STATUS_ERR , "unable to update user : "+e.getMessage() );
		}
		return new ServerReponse( ServerReponse.STATUS_OK , "ok" );
	 }
	
	@Override
	public List<User> getUser() 
	{        
		/*
		 * Recuperation d'args ?
		 */
		String userid = (String) getRequest().getAttributes().get("id");
		if( userid != null )
		{
			ArrayList<User> l = new ArrayList<User>(1);
			long luserid = 0;
			try
			{
				luserid = Long.parseLong(userid );
			}
			catch(Exception e){}
			
			if( luserid > 0 )
			{
				User u =  UserDao.getInstance().getUser( luserid );
				if( u != null )
				{
					l.add(u);
				}
			}
			return l;
		}
		else
		{
			String sort 	= getQuery().getValues("sort");
			String asc		= getQuery().getValues("asc");
			String start 	= getQuery().getValues("start");
			String length 	= getQuery().getValues("length");
			return  UserDao.getInstance().getAllUser(sort,asc,start,length);
		}
	 }

	
	@Override
	public ServerReponse deleteUser(  ) 
	{
		/*
		 * Recuperation d'args ?
		 */
		String userid = (String) getRequest().getAttributes().get("id");
		if( userid != null )
		{
			long luserid = 0;
			try
			{
				luserid = Long.parseLong(userid );
			}
			catch(Exception e)
			{
				return new ServerReponse( ServerReponse.STATUS_ERR , "No user with id '"+ userid +"' " );
			}
			
			if( luserid > 0 )
			{
				try
				{
					UserDao.getInstance().delete( luserid );
				}
				catch(Exception e)
				{
					return new ServerReponse( ServerReponse.STATUS_ERR , "A problem occured when trying to delete user '"+ userid +"' : "+e.getMessage() );
				}
			}
		}
		else
		{
			return  new ServerReponse( ServerReponse.STATUS_ERR , "No user with id '"+ userid +"' " );
		}
		
		return new ServerReponse( ServerReponse.STATUS_OK , "User '"+ userid +"' successfully deleted." );

	}
	

}
